import { reqCartList, reqDeleteCart,reqUpdateChecked } from "@/api"
const mutations={
    GETCARTLIST(state,a) {
      state.b=a
    }
}
const actions={
   async getCartList(context){
       let result=await reqCartList()
       if(result.code==200){
        context.commit('GETCARTLIST',result.data)
       }
    },
    async getDeleteCart(context,skuId){
        let result=await reqDeleteCart(skuId)
        if(result.code==200){
         return "ok"
        }else{
            return Promise.reject(new Error('faile'))
        }
     },
    async getUpdateChecked(context,{skuId,isChecked}){
        let result=await reqUpdateChecked(skuId,isChecked)
        if(result.code==200){
         return "ok"
        }else{
            return Promise.reject(new Error('faile'))
        }
     },
     deleteAllCart({dispatch,getters}){
         let promiseAll=[]
         getters.c.cartInfoList.forEach(item=> {
         let promise=item.isChecked==1?dispatch('getDeleteCart',item.skuId):''
         promiseAll.push(promise)
      });
      return Promise.all(promiseAll)
     },
     updateAll(context,isChecked){
         let promiseAll=[]
         context.getters.c.cartInfoList.forEach(
             item=>{
                  
                 let promise=context.dispatch('getUpdateChecked',{skuId:item.skuId,isChecked})
                 promiseAll.push(promise)
             }
         )
         return Promise.all(promiseAll)
     }
}
const state={b:[]}
const getters={
    c(state){
        return state.b[0]||{}
    }
}
export default{
    mutations,
    actions,
    state,
    getters
}